package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.models.ItemSetting;
import com.moleq.models.NetWT;
import com.moleq.models.VdrCoupon;

public class VdrCouponService {
	
	Context mContext;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public VdrCouponService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
		this.mContext = context;
	}

	public VdrCouponService(String shareDBPath) {
		this.db = SQLiteDatabase.openOrCreateDatabase(shareDBPath, null);
	}

	public List<VdrCoupon> findAll() {
		String sql = "Select * from " + DatabaseSchema.VdrCoupon.SCHEMA_NAME;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		List<VdrCoupon> Source = new ArrayList<VdrCoupon>();
		if (cursor == null) {
			return Source;
		}
		while (cursor.moveToNext()) {
			VdrCoupon vdrCoupon = new VdrCoupon();
			vdrCoupon.setCv_code_no(cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_CODE_NO)));
			vdrCoupon.setCv_desc(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_DESC)));
			vdrCoupon.setCv_free_qty(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_FREE_QTY)));
			vdrCoupon.setCv_min_qty(cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_MIN_QTY)));
			vdrCoupon.setCv_more_fl(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_MORE_FL)));
			vdrCoupon.setCv_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_VALUE)));
			Source.add(vdrCoupon);
		}
		cursor.close();
		return Source;
	}
	
	public VdrCoupon findVdrCouponById(String no) {
		String sql = "Select * from " + DatabaseSchema.VdrCoupon.SCHEMA_NAME + " where " + DatabaseSchema.VdrCoupon.CV_CODE_NO + " = " + no;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		VdrCoupon Source = new VdrCoupon();
		if (cursor == null) {
			return Source;
		}
		if (cursor.moveToNext()) {
			Source.setCv_code_no(cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_CODE_NO)));
			Source.setCv_desc(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_DESC)));
			Source.setCv_free_qty(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_FREE_QTY)));
			Source.setCv_min_qty(cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_MIN_QTY)));
			Source.setCv_more_fl(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_MORE_FL)));
			Source.setCv_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.VdrCoupon.CV_VALUE)));
		}
		cursor.close();
		return Source;
	}
}
